//
//  GRXTabBar.m
//  01-BuDeJie
//
//  Created by xmg on 15/12/17.
//  Copyright © 2015年 grx. All rights reserved.
//

#import "GRXTabBar.h"



@interface GRXTabBar ()

/** 加号按钮*/
@property (nonatomic ,weak) UIButton *plusButton;

@end

@implementation GRXTabBar

//懒加载
- (UIButton *)plusButton{
    if (_plusButton == nil) {
        UIButton *plusButton = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [plusButton setImage:[UIImage imageNamed:@"topic_post_icon"] forState:UIControlStateNormal];
        [plusButton setImage:[UIImage imageNamed:@"topic_post_icon"] forState:UIControlStateHighlighted];
        
        //自适应尺寸,根据图片或者文字计算大小
//        [plusButton sizeToFit];
//        plusButton.bounds.size = CGSizeMake(37, 37);
        plusButton.frame = CGRectMake(0, 0, 37, 37);
        
        [plusButton addTarget:self action:@selector(plusButtonClick:) forControlEvents:UIControlEventTouchDown];
        
        _plusButton = plusButton;
        
        [self addSubview:plusButton];
    }
    return _plusButton;
}


- (void)plusButtonClick:(UIButton *)btn{
    NSLog(@"打开相机");
}



//子控件已经添加进去了
- (void)layoutSubviews{
    [super layoutSubviews];
    
//    NSLog(@"%zd",self.items.count);
    //跳转子控件布局
    NSInteger count = self.items.count + 1;
    
    CGFloat btnW = self.width / (count * 1.0);
    CGFloat btnH = self.height;
    CGFloat btnX = 0;
    CGFloat btnY = 0;
    
    int i = 0;
    for (UIView *tabBtn in self.subviews) {
        if ([tabBtn isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            if (i == 2) {
                i += 1;
            }
            
            btnX = i * btnW;
            
            tabBtn.frame = CGRectMake(btnX, btnY, btnW, btnH);
            
            i++;
        }
    }
    
    //设置按钮居中
    self.plusButton.center = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.5);
}




@end
